package org.example.demo2.controller;

import jakarta.servlet.ServletOutputStream;
import jakarta.servlet.http.HttpServletResponse;
import org.example.demo2.common.PageData;
import org.example.demo2.common.ResponseData;
import org.example.demo2.pojo.*;
import org.example.demo2.service.IFileService;
import org.example.demo2.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URLEncoder;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.UUID;

@RestController
public class UserController {

    private static String UPLOADED_FOLDER = "D:\\test\\";
    @Autowired
    IUserService userService;
    @Autowired
    IFileService fileService;

    //注册接口
    @PutMapping("/signUser")
    public ResponseData<UserInfo> signUser(@RequestBody UserInfo userInfo){
        return userService.signUser(userInfo);
    }

    //登录接口
    @PostMapping("/loginUser")
    public ResponseData<UserLogin> loginUser(@RequestBody UserLogin userlogin){
        return userService.loginUser(userlogin);
    }

    @PostMapping("/getUser")
    public ResponseData<UserInfo> getUser(@RequestParam("userName") String userName){
        return userService.getUser(userName);
    }

    @DeleteMapping("/deleteUser")
    public ResponseData<UserInfo> deleteUser(@RequestParam("userName") String userName){
        return userService.deleteUser(userName);
    }

    @PostMapping("/page")
    public ResponseData<PageData<UserInfo>> page(@RequestBody UserQuery userQuery){
        return userService.page(userQuery);
    }

    @PutMapping("updataStu")
    public ResponseData<StuInfo> updateStu(@RequestBody StuInfo stuInfo){
        return userService.updateStu(stuInfo);
    }

    @PostMapping("uploadFile")
    public ResponseData<String> uploadFile(@RequestParam("file") MultipartFile file){
        if(file.isEmpty()){
            return ResponseData.fail("文件不能为空");
        }
        try{
            byte[] bytes= file.getBytes();
            String fileName= file.getOriginalFilename();
            String extName=fileName.substring(fileName.lastIndexOf("."));
            String fileId= UUID.randomUUID().toString();
            String filePath=UPLOADED_FOLDER+fileId+extName;
            Path path= Paths.get(filePath);
            Files.write(path,bytes);
            FileInfo fileInfo=new FileInfo();
            fileInfo.setFileId(fileId);
            fileInfo.setFileName(fileName);
            fileInfo.setFilePath(filePath);
            fileService.upload(fileInfo);
            return ResponseData.success(fileId,"ok");
        } catch (IOException e) {
            return ResponseData.fail("文件上传失败");
        }
    }

    @GetMapping("/downFile")
    public void downFile(@RequestParam("fileId")String fileId, HttpServletResponse response)throws IOException{
        FileInfo fileInfo = fileService.getFile(fileId);
        InputStream inputStream = new FileInputStream(fileInfo.getFilePath());
        response.reset();
        response.setContentType("appLication/octet-stream");
        String filename=fileInfo.getFileName();
        response.addHeader("Content-Disposition","attachment:filename="+ URLEncoder.encode(filename,"UTF-8"));
        ServletOutputStream outputStream=response.getOutputStream();
        byte[] b =new byte[1024];
        int length;
        while ((length=inputStream.read(b)) > 0){
            outputStream.write(b,0,length);
        }
        inputStream.close();
    }
}













